import Vue from 'vue'
import VueRouter from 'vue-router'
import config from './config'

import template_content from '@/pages/template/content'

import login from '@/pages/login/index'
import index from '@/pages/index/index'

Vue.use(VueRouter)

const router = new VueRouter({
	mode: 'hash',
	routes: [{
		path: '/login',
		component: login,
		name: 'login'
	}, {
		path: '/',
		component: index,
		name: 'index',
		children: [{
			path: 'template_content',
			component: template_content,
			name: 'template_content'
		}]
	}]
});

/**
 * 导航守卫
 */
router.beforeEach((to, from, next) => {
	if (to.name != 'login') {
		let token = localStorage.getItem(config.tokenName)
		if (!token) {
			next({
				name: 'login',
				replace: true
			})
			return;
		}
	}
	next()
});

export default router;
